package com.example.demo.utils;

import org.springframework.stereotype.Component;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
@Component
public class EncryptionUtils {

    public static String encryptPassword(String password) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] hash = md.digest(password.getBytes());
            return Base64.getEncoder().encodeToString(hash);
        } catch (NoSuchAlgorithmException e) {
            // 处理加密算法不存在的情况
            throw new RuntimeException("Encryption algorithm not found", e);
        }
    }

    public static boolean matches(String rawPassword, String encodedPassword) {
        String encryptedPassword = encryptPassword(rawPassword);
        return encryptedPassword.equals(encodedPassword);
    }
}
